package com.lecyon.farm.service.impl;

import com.google.common.collect.Lists;
import com.lecyon.farm.common.constant.CommonConstant;
import com.lecyon.farm.entity.elasticsearch.GpsPictureInformation;
import com.lecyon.farm.mapper.GpsPictureInformationRepository;
import com.lecyon.farm.service.IGpsPictureInformationService;
import com.lecyon.farm.util.StringHelper;
import com.lecyon.farm.vo.query.QueryGpsPictureInformationVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * GPS图片信息 服务实现类
 *
 * @author Yao Zheng
 * @Date 2020/3/23 18:23
 */
@Service
@Slf4j
public class GpsPictureInformationServiceImpl implements IGpsPictureInformationService {

    private GpsPictureInformationRepository gpsPictureInformationRepository;

    private ElasticsearchTemplate elasticsearchTemplate;

    private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(CommonConstant.DATE_TIME_FORMAT_PATTERN);

    /**
     * 根据条件查询GPS图片信息
     *
     * @param queryVo 查询条件
     * @return GPS图片信息
     */
    @Override
    public List<GpsPictureInformation> getList(QueryGpsPictureInformationVo queryVo) {
        String beginTime = queryVo.getBeginTime();
        String endTime = queryVo.getEndTime();
        String carId = queryVo.getCarId();
        List<GpsPictureInformation> resultList;
        resultList = gpsPictureInformationRepository.findByCarIdAndTimeBetween(carId, beginTime, endTime);
        List<GpsPictureInformation> result = Lists.newArrayList();
        LocalDateTime begin = LocalDateTime.parse(beginTime, dateTimeFormatter);
        LocalDateTime end = LocalDateTime.parse(endTime, dateTimeFormatter);
        LocalDateTime recordTime;
        if (CollectionUtils.isNotEmpty(resultList)) {
            for (GpsPictureInformation entity : resultList) {
                if (StringHelper.isEmpty(entity.getCarId()) || !carId.equals(entity.getCarId())) {
                    continue;
                }
                if (StringHelper.isNotEmpty(entity.getTime())) {
                    recordTime = LocalDateTime.parse(entity.getTime(), dateTimeFormatter);
                    if (recordTime.isBefore(begin) || recordTime.isAfter(end)) {
                        continue;
                    }
                }
                result.add(entity);
            }
        }
        if (CollectionUtils.isNotEmpty(result)) {
            result = result.stream().distinct().collect(Collectors.toList());
            for (GpsPictureInformation entity : result) {
                log.info(entity.toString());
            }
        }
        return result;
    }

    /**
     * ----------------------------- 分割线 以下是setter方法，用于替换field上的@Autowired -----------------------------
     */

    @Autowired
    @Lazy
    public void setGpsPictureInformationRepository(GpsPictureInformationRepository gpsPictureInformationRepository) {
        this.gpsPictureInformationRepository = gpsPictureInformationRepository;
    }

    @Autowired
    @Lazy
    public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
        this.elasticsearchTemplate = elasticsearchTemplate;
    }
}
